
import { Logger,LogFactory } from "../common/log";
import { RPC } from "../common/rpc";

async function main(){
    LogFactory.inst().init(process.cwd() + "/logs/", "log");
    let log:Logger = LogFactory.inst().getLogger("TestRPC");

    let rpc = new RPC("localhost", 6379);
    rpc.listen("login:rpc", (method:string, param:object):object=>{
        switch(method){
            case "getUserToken":
                    return {code:"ok",token:"12344"};
            break;
        }
        return {code:"fail", msg:"no rpc method"};
    })

    rpc.listen("task:rpc", (method:string, param:object):object=>{
        switch(method){
            case "getUserTasks":
                return { code: "ok", id: (param as any).id, tasks: [] };
            break;
        }
        return {code:"fail", msg:"no rpc method"};
    })

    let reply = await rpc.call("login:rpc", "getUserToken", { name: "liliwang" }, 1).catch((err)=>{});
    if (reply) {
        log.info("ret:", reply);
    }


    for(let i=0; i<100; i++){
        let reply = await rpc.call("task:rpc", "getUserTasks", { name: "liliwang", id:i }, 1).catch((err) => {});
        if (reply) {
            log.info("ret:", reply);
        }
    }
}
main()